package com.vtuzx.kingdee.xqc_core.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.constant.IJikeyunConst;
import com.vtuzx.kingdee.xqc_core.utils.JiKeYunUtil;
import com.vtuzx.kingdee.xqc_core.dao.StoreTokenDao;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 吉克云api
 */
@Service
public class IJiKeYunApiService {

    @Autowired
    private StoreTokenDao storeTokenDao;

    /**
     * 调用API
     */
    public VtuzxMap callApi(String method, Document integrationStore, Map<String, Object> param, boolean isBatch) throws Exception {
        return JiKeYunUtil.callApi(
                VtuzxUtil.getBoolean(integrationStore, "prod"),
                VtuzxUtil.getLong(integrationStore, IJikeyunConst.Key_app_key),
                VtuzxUtil.getString(integrationStore, IJikeyunConst.Key_app_secret),
                method,
                param,
                isBatch);
    }
    /**
     * 批量创建货品
     */
    public VtuzxMap createGoods(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.goods.skuimportbatch", storeToken, param,true);
    }

    /**
     * 传入唯一码
     * erp.serialno.add
     */
    public VtuzxMap serialNoAdd(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.serialno.add", storeToken, param, false);
    }
    /**
     * 客户档案列表
     */
    public VtuzxMap customerList(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("crm.customer.list", storeToken, param, false);
    }


    /**
     * 调拨查询
     */
    public VtuzxMap allocationList(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.allocate.get", storeToken, param, false);
    }


    /**
     * 库存查询
     */
    public VtuzxMap skuList(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp-stock.stock.skulist", storeToken, param, false);
    }


    /**
     * 分页获取物料(主要获取条码)
     * erp.storage.goodslist
     */
    public VtuzxMap getGoodsList(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.storage.goodslist", storeToken, param, false);
    }

    /**
     * 创建极客云入库单并申请入库
     * erp.stock.createandstockin
     */
    public VtuzxMap createStockIn(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.stock.createandstockin", storeToken, param, false);
    }

    /**
     * 创建极客云出库单并申请出库
     * erp.stock.createandstockin
     */
    public VtuzxMap createStockOut(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.stock.createandstockout", storeToken, param, false);
    }

    /**
     * 创建极客云出库单申请
     * erp.storage.stockoutcreate
     */
    public VtuzxMap createStock(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.storage.stockoutcreate", storeToken, param, false);
    }

    /**
     * 创建极客云入库申请单
     * erp.storage.stockoutcreate
     */
    public VtuzxMap createStockInApply(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.storage.stockincreate", storeToken, param, false);
    }


    /**
     * 获取极客云收款单和退款单
     * erp.stock.createandstockin
     */
    public VtuzxMap getPayAndCol(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("fin-fbs.billinfo.allreceivepaymentlist", storeToken, param, false);
    }
    /**
     * 销售单查询
     */
    public VtuzxMap getSaleBillOld(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("oms.trade.fullinfoget", storeToken, param, false);
    }
    //销售单查询(定制)
    public VtuzxMap getSaleBill(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("jackyun.tradenotsensitiveinfos.list.get", storeToken, param, false);
    }
    //库存查询
    public VtuzxMap getInventory(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.stockquantity.get", storeToken, param, false);
    }

    //查询价目表
    public VtuzxMap getPriceList(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.pricelist.get", storeToken, param, false);
    }

//    public VtuzxMap getSaleBill(Document storeToken, VtuzxMap param) throws Exception {
//        return callApi("oms.trade.fullinfoget.customized", storeToken, param, false);
//    }
    /**
     * 公司间结算查询
     */
    public VtuzxMap getInnerOrder(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.innersettorder.get", storeToken, param, false);
    }
    /**
     * 结算单查询
     */
    public VtuzxMap getTradeSettleAllInfo(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("oms.open.trade.settle.allinfo", storeToken, param, false);
    }
    /**
     * 销售渠道查询
     */
    public VtuzxMap getSales(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.sales.get", storeToken, param, false);
    }
    /**
     * 公司间结算货品信息查询
     */
    public VtuzxMap getInnerOrderGoods(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.innersettorder.gooddetail.get", storeToken, param, false);
    }

    /**
     * 出库单查询(其它出库)
     */
    public VtuzxMap getStorageOut(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.storage.goodsdocout.v2", storeToken, param, false);
    }

    /**
     * 批量新增箱子
     */
    public VtuzxMap createBoxBatch(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("wms.batch.createboxbatch", storeToken, param, true);
    }

    /**
     * 查询仓库信息
     */
    public VtuzxMap getStock(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.warehouse.get", storeToken, param, false);
    }

    /**
     * 开放平台查询唯一码接口
     */
    public VtuzxMap getSerialNo(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.serialno.get", storeToken, param, false);
    }

    /**
     * 查询经销商
     */
    public VtuzxMap getDealer(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.sales.get", storeToken, param, false);
    }

    /**
     * 查询经销商
     */
    public VtuzxMap getSeller(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.user.search", storeToken, param, false);
    }


    /**
     * 查询发货单(未完成的)
     */
    public VtuzxMap getSaleOrderInfo(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("wms.order.query-info.page.v2", storeToken, param, false);
    }

    /**
     * 查询发货单(以完成的)
     */
    public VtuzxMap getSaleOrderInfoFinish(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("wms.order.query-info.page", storeToken, param, false);
    }



    /**
     * 根据发货单号查询包裹
     */
    public VtuzxMap getPackageListByOrderNo(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("wms.order.packlist", storeToken, param, false);
    }

    /**
     * 销售渠道查询
     */
    public VtuzxMap getChannel(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.sales.get", storeToken, param, false);
    }

    /**
     * 存货仓库查询
     */
    public VtuzxMap getWarehouse(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.warehouse.get", storeToken, param, false);
    }

    //组合装查询
    public VtuzxMap getGoodsPackage(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp-goods.goods.listgoodspackage", storeToken, param, false);
    }

    //入库查询
    public VtuzxMap getStorageGoodsdocin(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.storage.goodsdocin.v2", storeToken, param, false);
    }

    /**
     * 调拨单创建
     */
    public VtuzxMap allocatteCreate(Document storeToken, VtuzxMap param) throws Exception {
        return callApi("erp.allocate.create", storeToken, param, false);
    }
}
